clear all
clc
close all

if 1
    for e=1:1

        if rand>.5
            dcFile='dcl.wav';
            dcBegin=.67875;
            dcEnd=30.7;

            pieces=64;
        else
            dcFile='dc.wav';
            dcBegin=1.63;
            dcEnd=16.62;
            pieces=32;
        end

        %[dcX dcRate]=wavread('dcl.wav');
        [dcX dcRate]=wavread(dcFile);
        dcX=dcX(:,1);
        dcX=2*(dcX-min(dcX))/range(dcX) -1;

        %dcBegin=1.63;
        %dcEnd=16.62;

        %dcBegin=.67875;
        %dcEnd=30.7;
        %     dclX=audioplayer(dcX(round([25*dcRate:dcEnd*dcRate  dcBegin*dcRate:3*dcRate ])),dcRate);
        %     for i=1:3
        %     playblocking(dclX);
        %     end

        %pieces=32;
        %pieces=64;
        dcInds=linspace(dcBegin*dcRate,dcEnd*dcRate,pieces+1);
        dcInds=round([dcInds(1:end-1);dcInds(2:end)]);

        'gg'
        for gg=1:2

            for i=1:pieces
                lx=rand*dcRate/2;
                hx=rand*.05*lx;
                dcAP{i}=audioplayer(bandPass(dcX(dcInds(1,i):dcInds(2,i)),lx,hx,dcRate),dcRate);
            end

            for ghy=1:1
                for i=1:pieces
                    playblocking(dcAP{i});
                end
            end
            for i=1:16
                playblocking(dcAP{ceil(rand*pieces)});
            end
        end



        [cnkX cnkRate]=wavread('cnk.wav');
        cnkX=cnkX(:,1);
        cnkX=2*(cnkX-min(cnkX))/range(cnkX) -1;
        cnkBegin=1.26;
        cnkEnd=50.65;

        cnkAP=audioplayer(cnkX(round(cnkBegin*cnkRate:cnkEnd*cnkRate)),cnkRate);
        'cnkap'
        for i=1:1
            playblocking(cnkAP);
        end


        pieces=240;
        cnkInds=linspace(cnkBegin*cnkRate,cnkEnd*cnkRate,pieces+1);
        cnkInds=round([cnkInds(1:end-1);cnkInds(2:end)]);

        numcnkFs=17;
        cnkFreqs=100*2.^(fliplr([0:numcnkFs])/numcnkFs);
        cnklo=rand*cnkRate/2;
        cnkhi=rand*(rand*cnklo/(1+rand*10));
        toneDur=.06;
        amp=.2;

        'freqs'
        for c=1:length(cnkFreqs)
            cnkD{c}=audioplayer(pulseWidthSnd(cnkRate,toneDur,cnkFreqs(c),rand,rand,cnklo,cnkhi).*amp,cnkRate);
        end

        clear cnkAP
        'pcs'
        for i=1:pieces
            cnkAP{i}=audioplayer(cnkX(cnkInds(1,i):cnkInds(2,i)),cnkRate);
        end

        'cnkd'
        for i=1:pieces
            play(cnkD{1+mod(i-1,numcnkFs-1)});
            playblocking(cnkAP{i});
            if mod(i,numcnkFs-1)==0
                cnkD=fliplr(cnkD);
            end
        end

    end

    'cnkap'
    for i=1:1
        playblocking(cnkAP{ceil(rand*pieces)});
    end

end



if ~ismac
cd 'mp3'
fn='..\0419_baldwin.mp3';
[baldwinX baldwinSampRate]=mp3read(fn);
cd ..
baldwinX=baldwinX(:,1);
end

ind=0;
for i=1:100

    if rand<.3 && ~ismac
        fn='..\0419_baldwin.mp3';
        x = baldwinX;
        sampRate = baldwinSampRate;
    else
        if rand>.5
            fn='broke_it.wav';
        else
            fn='amen_1.wav';
        end
        [x,sampRate,nbits,opts]=wavread(fn);
    end
    x=2*(x-min(x)/range(x))-1;

    fprintf('loaded %s\n',fn)

    numPlayers = ceil(7*rand);

    for i=1:numPlayers
        numTones=ceil(rand*4);
        loPass=rand*sampRate/2;
        hiPass=rand*(rand*loPass/(1+rand*10));
        a{i}=audioplayer(pulseWidthSnd(sampRate,rand*.25,75*10.^rand(1,numTones),rand(1,numTones)/abs(10*sin(i)+rand),rand,loPass,hiPass),sampRate);
    end

    numcycs = round(20*rand);
    for i=1:numcycs
        play(a{ceil(rand*numPlayers)});
        WaitSecs(rand*.2)
        for j=1:floor(rand*10)
            if rand<.35
                fprintf('playing filtered sample\n')

                dur=ceil(rand*.8*sampRate);
                start=ceil(rand*(length(x)-dur));

                filted=bandPass(x(start:start+dur),loPass,hiPass,sampRate);
                filted=2*((filted-min(filted))/range(filted))-1;
                ind=mod(ind,2)+1;
                q{ind}=audioplayer(filted,sampRate);
                play(q{ind});
                WaitSecs(rand*dur/sampRate)
            end

            if rand<.01
                fprintf('playing random tones\n')
                for k=1:ceil(rand*4)
                    freqs=(40+rand*150)*2.^([0:12]/12);
                    freqs=freqs([1 2 3 4 6 7 8 9 11 12 13]);
                    freqs=[freqs(end-1)/2 freqs 2*freqs(3)];
                    if rand<.3
                        freqs=fliplr(freqs);
                    end
                    lo=rand*sampRate/2;
                    hi=rand*(rand*lo/(1+rand*10));
                    for c=1:length(freqs)
                        d{c}=audioplayer(pulseWidthSnd(sampRate,.06,freqs(c),rand,rand,lo,hi),sampRate);
                        playblocking(d{c});
                    end
                    if rand<.3
                        for w=1:ceil(rand*100)
                            playblocking(d{ceil(rand*length(freqs))});
                        end
                    end
                end

            end

            if rand<.01
                fprintf('playing beats\n')

                f=pulseWidthSnd(sampRate,rand*.6,60+rand*60,rand,rand,rand*sampRate/2,0);
                spots=logspace(.001,rand*2,length(f));
                if rand>.5
                    fliplr(spots);
                end
                env=normpdf(spots,spots(ceil(rand*length(spots))),rand*3).^(rand*3.5);
                f=f.*env;
                f=2*((f-min(f))/range(f))-1;
                ap=audioplayer(f,sampRate);
                for v=1:ceil(rand*10)
                    playblocking(ap);
                end
            end

            if rand<.015
                fprintf('playing scratched\n')

                h=10*randn(1,ceil(rand*.2*length(x)));
                h=bandPass(h,1+rand*10,0,sampRate);
                h=rand*length(x)+ cumsum(h);
                h=h+rand*randn*(1:length(h));
                h=round(h);
                h=mod(h,length(x))+1;
                playblocking(audioplayer(bandPass(x(h),sampRate/6,20,sampRate),sampRate));
            end

            if rand<.04 && strcmp(fn,'amen_1.wav')
                fprintf('playing breaks\n')

                old=x;
                speed=.5+rand*1.5;
                x=ceil(1:speed:length(x));
                x=old(x);
                numChunks=16;
                t=ceil(linspace(1,length(x),numChunks+1));
                ts=ceil(rand(1,ceil(rand*50))*(length(t)-1));
                for ti=1:numChunks
                    lx=rand*sampRate/2;
                    hx=rand*.05*lx;
                    ftx=bandPass(x(t(ti):floor(t(ti)+length(x)/numChunks)),lx,hx,sampRate);
                    tas{ti}=audioplayer(ftx,sampRate);
                end
                for ti=1:length(ts)
                    playblocking(tas{ts(ti)});
                end
                x=old;
            end

            play(a{ceil(rand*numPlayers)});
            WaitSecs(rand*.2)

            if rand<.015
                fprintf('playing vocoded sample\n')

                dur=ceil(rand*4*sampRate);
                start=ceil(rand*(length(x)-dur));
                z=vocode(x(start:start+dur),pulseWidthSnd(sampRate,dur/sampRate,75*10.^rand(1,numTones),rand(1,numTones)/abs(10*sin(i)+rand),rand,loPass,hiPass),int8(ceil(rand*20)),sampRate);
                ind=mod(ind,2)+1;
                q{ind}=audioplayer(z,sampRate);
                for p=1:ceil(rand*10)
                    playblocking(q{ind});
                end
                play(q{ind});
                WaitSecs(rand*dur/sampRate)
            end
        end
    end

end